/*
 * Copyright (C) 2016 venshine.cn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wx.wheelview.demo;

import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.adapter.SimpleWheelAdapter;
import com.wx.wheelview.common.WheelConstants;
import com.wx.wheelview.common.WheelData;
import com.wx.wheelview.util.WheelUtils;
import com.wx.wheelview.widget.WheelView;
import com.wx.wheelview.widget.WheelViewDialog;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * Demo
 *
 * @author venshine
 */
public class MainAbility extends Ability {
    private int mScreenWidth;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(Color.getIntColor("#303F9F"));
        setUIContent(ResourceTable.Layout_ability_main);

        mScreenWidth = WheelUtils.getDisplayWidthInPx(this);
        DirectionalLayout mainLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_main_layout);
        mainLayout.setHeight(WheelUtils.getDisplayHeightInPx(this));

        initWheel1();
        initWheel2();
        initWheel3();

        findComponentById(ResourceTable.Id_bt_dialog).setClickedListener(
                component -> showDialog(component));
    }

    /**
     * 联动WheelView
     */
    private void initWheel1() {
        int wheel1Margin = WheelUtils.dip2px(getContext(),
                WheelUtils.parseDimension(getContext().getString(ResourceTable.Float_wheel1_margin)));

        WheelView mainWheelView = (WheelView) findComponentById(ResourceTable.Id_main_wheelview);
        mainWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        mainWheelView.setWidth(mScreenWidth / 3 - 2 * wheel1Margin);
        mainWheelView.setHeight(mainWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        mainWheelView.setSkin(WheelView.Skin.Holo);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextSize = 20;
        style.textSize = 16;
        mainWheelView.setStyle(style);
        mainWheelView.setWheelData(createMainDatas());

        WheelView subWheelView = (WheelView) findComponentById(ResourceTable.Id_sub_wheelview);
        subWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        subWheelView.setWidth(mScreenWidth / 3 - 2 * wheel1Margin);
        subWheelView.setHeight(subWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        subWheelView.setSkin(WheelView.Skin.Holo);
        subWheelView.setStyle(style);
        mainWheelView.join(subWheelView);
        mainWheelView.joinDatas(createSubDatas());
        subWheelView.setWheelData(createSubDatas().get(createMainDatas().get(mainWheelView.getSelection())));

        WheelView childWheelView = (WheelView) findComponentById(ResourceTable.Id_child_wheelview);
        childWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        childWheelView.setWidth(mScreenWidth / 3 - 2 * wheel1Margin);
        childWheelView.setHeight(childWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        childWheelView.setSkin(WheelView.Skin.Holo);
        childWheelView.setStyle(style);
        subWheelView.join(childWheelView);
        subWheelView.joinDatas(createChildDatas());
        childWheelView.setWheelData(createChildDatas().get(createSubDatas().get(createMainDatas().get(
                mainWheelView.getSelection())).get(subWheelView.getSelection())));
    }

    /**
     * holo皮肤
     */
    private void initWheel2() {
        int wheel2Margin = WheelUtils.dip2px(getContext(),
                WheelUtils.parseDimension(getContext().getString(ResourceTable.Float_wheel2_margin)));

        WheelView hourWheelView = (WheelView) findComponentById(ResourceTable.Id_hour_wheelview);
        hourWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        hourWheelView.setWidth(mScreenWidth / 3 - 2 * wheel2Margin);
        hourWheelView.setHeight(hourWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        hourWheelView.setSkin(WheelView.Skin.Holo);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.getIntColor("#0288ce");
        style.textColor = Color.GRAY.getValue();
        style.selectedTextSize = 20;
        style.selectedTextBold = true;
        hourWheelView.setStyle(style);
        hourWheelView.setExtraText("时", Color.getIntColor("#0288ce"), 40, 70, true);
        hourWheelView.setWheelData(createHours());

        WheelView minuteWheelView = (WheelView) findComponentById(ResourceTable.Id_minute_wheelview);
        minuteWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        minuteWheelView.setWidth(mScreenWidth / 3 - 2 * wheel2Margin);
        minuteWheelView.setHeight(hourWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        minuteWheelView.setSkin(WheelView.Skin.Holo);
        minuteWheelView.setStyle(style);
        minuteWheelView.setExtraText("分", Color.getIntColor("#0288ce"), 40, 70, true);
        minuteWheelView.setWheelData(createMinutes());

        WheelView secondWheelView = (WheelView) findComponentById(ResourceTable.Id_second_wheelview);
        secondWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        secondWheelView.setWidth(mScreenWidth / 3 - 2 * wheel2Margin);
        secondWheelView.setHeight(hourWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        secondWheelView.setSkin(WheelView.Skin.Holo);
        secondWheelView.setStyle(style);
        secondWheelView.setExtraText("秒", Color.getIntColor("#0288ce"), 40, 70, true);
        secondWheelView.setWheelData(createMinutes());
    }

    /**
     * common皮肤、图文混排无皮肤、自定义布局
     */
    private void initWheel3() {
        int wheel3Margin = WheelUtils.dip2px(getContext(),
                WheelUtils.parseDimension(getContext().getString(ResourceTable.Float_wheel3_margin)));

        WheelView commonWheelView = (WheelView) findComponentById(ResourceTable.Id_common_wheelview);
        commonWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        commonWheelView.setWidth(mScreenWidth / 3 - 2 * wheel3Margin);
        commonWheelView.setHeight(commonWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        commonWheelView.setSkin(WheelView.Skin.Common);
        commonWheelView.setWheelData(createArrays());

        WheelView simpleWheelView = (WheelView) findComponentById(ResourceTable.Id_simple_wheelview);
        simpleWheelView.setWheelAdapter(new SimpleWheelAdapter(this));
        simpleWheelView.setWheelSize(5);
        simpleWheelView.setWidth(mScreenWidth / 3 - 2 * wheel3Margin);
        simpleWheelView.setHeight(simpleWheelView.getWheelSize() * WheelUtils.dip2px(
                getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
        simpleWheelView.setSkin(WheelView.Skin.None);
        simpleWheelView.setLoop(true);
        simpleWheelView.setWheelClickable(true);
        simpleWheelView.setWheelData(createDatas());
        simpleWheelView.setOnWheelItemClickListener((position, item) -> {
            WheelUtils.log("click:" + Integer.valueOf(((WheelData) item).getName()));
        });
        simpleWheelView.setOnWheelItemSelectedListener((position, item) -> {
            WheelUtils.log("selected:" + Integer.valueOf(((WheelData) item).getName()));
        });

        WheelView myWheelView = (WheelView) findComponentById(ResourceTable.Id_my_wheelview);
        myWheelView.setWheelAdapter(new MyWheelAdapter(this));
        myWheelView.setWheelSize(5);
        myWheelView.setSelection(2);
        myWheelView.setClickToPosition(true);
        myWheelView.setWidth(mScreenWidth / 3 - 2 * wheel3Margin);
        myWheelView.setHeight(simpleWheelView.getWheelSize() * WheelUtils.dip2px(getContext(), 50));
        myWheelView.setSkin(WheelView.Skin.Holo);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.backgroundColor = Color.YELLOW.getValue();
        style.textColor = Color.DKGRAY.getValue();
        style.selectedTextColor = Color.GREEN.getValue();
        myWheelView.setStyle(style);
        myWheelView.setWheelData(createArrays());
    }

    private void showDialog(Component view) {
        WheelViewDialog dialog = new WheelViewDialog(MainAbility.this);
        dialog.setTitle("wheelview dialog").setButtonText("确定").setDialogStyle(Color
                .getIntColor("#6699ff")).setCount(5).setHeight().show();
        dialog.setItems(createArrays());
    }

    /**
     * createMainDatas
     *
     * @return 省份信息
     */
    public List<String> createMainDatas() {
        String[] strings = {
                "黑龙江",
                "吉林",
                "辽宁"
        };
        return Arrays.asList(strings);
    }

    /**
     * createSubDatas
     *
     * @return 省份和市的对应关系
     */
    public HashMap<String, List<String>> createSubDatas() {
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        String[] strings = {
                "黑龙江",
                "吉林",
                "辽宁"
        };
        String[] s1 = {
                "哈尔滨",
                "齐齐哈尔",
                "大庆"
        };
        String[] s2 = {
                "长春",
                "吉林"
        };
        String[] s3 = {
                "沈阳",
                "大连",
                "鞍山",
                "抚顺"
        };
        String[][] ss = {
                s1,
                s2,
                s3
        };
        for (int i = 0; i < strings.length; i++) {
            map.put(strings[i], Arrays.asList(ss[i]));
        }
        return map;
    }

    /**
     * createChildDatas
     *
     * @return 市和县的对应关系
     */
    public HashMap<String, List<String>> createChildDatas() {
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        String[] strings = {"哈尔滨", "齐齐哈尔", "大庆", "长春", "吉林",
                "沈阳", "大连", "鞍山", "抚顺"
        };
        String[] s1 = {"道里区", "道外区", "南岗区", "香坊区"};
        String[] s2 = {"龙沙区", "建华区", "铁锋区"};
        String[] s3 = {"红岗区", "大同区"};
        String[] s11 = {"南关区", "朝阳区"};
        String[] s12 = {"龙潭区"};
        String[] s21 = {"和平区", "皇姑区", "大东区", "铁西区"};
        String[] s22 = {"中山区", "金州区"};
        String[] s23 = {"铁东区", "铁西区"};
        String[] s24 = {"新抚区", "望花区", "顺城区"};
        String[][] ss = {
                s1,
                s2,
                s3,
                s11,
                s12,
                s21,
                s22,
                s23,
                s24
        };
        for (int i = 0; i < strings.length; i++) {
            map.put(strings[i], Arrays.asList(ss[i]));
        }
        return map;
    }

    /**
     * createHours
     *
     * @return 小时数组
     */
    public ArrayList<String> createHours() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add("" + i);
            }
        }
        return list;
    }

    /**
     * createMinutes
     *
     * @return 分钟数组
     */
    public ArrayList<String> createMinutes() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add("" + i);
            }
        }
        return list;
    }

    /**
     * createArrays
     *
     * @return 普通的数据
     */
    public ArrayList<String> createArrays() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("item" + i);
        }
        return list;
    }

    /**
     * createDatas
     *
     * @return WheelData数据
     */
    public ArrayList<WheelData> createDatas() {
        ArrayList<WheelData> list = new ArrayList<>();
        WheelData item;
        for (int i = 0; i < 20; i++) {
            item = new WheelData();
            item.setId(ResourceTable.Media_icon);
            item.setName((i < 10) ? ("0" + i) : ("" + i));
            list.add(item);
        }
        return list;
    }
}
